
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wiser.ohoscard.utils;

import com.wiser.ohoscard.ResourceTable;
import com.wiser.ohoscard.db.Form;
import ohos.agp.components.ComponentProvider;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.Calendar;

/**
 * Component ProviderUtils
 */
public class ComponentProviderUtils {

    private static final int DIM_VERSION = 3;

    /**
     * Obtains the ComponentProvider object
     *
     * @param form form info
     * @param context context
     * @return component provider
     */
    public static ComponentProvider getComponentProvider(Form form, Context context) {
        int layoutId = ResourceTable.Layout_form_ability_layout_2_2;
        if (form.getDimension() == DIM_VERSION) {
            layoutId = ResourceTable.Layout_form_ability_layout_2_4;
        }
        ComponentProvider componentProvider = new ComponentProvider(layoutId, context);
        setComponentProviderValue(componentProvider);
        return componentProvider;
    }

    /**
     * Set the value of componentProvider
     *  更新UI
     * @param componentProvider component provider
     */
    private static void setComponentProviderValue(ComponentProvider componentProvider) {
        Calendar now = Calendar.getInstance();
        int second = now.get(Calendar.SECOND);
        componentProvider.setText(ResourceTable.Id_text_timer, String.valueOf(second));
        componentProvider.setTextColor(ResourceTable.Id_text_timer, Color.RED);
    }
}
